<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午5:41
 */
class ShopcartController extends Controller {

    private $_nowtime;
    private $_nowdate;
    private $_logininfo;

    public function init() {
        parent::init();
        $this->_logininfo = $this->_session->offsetGet('loginInfos');
        $__now_time = $_SERVER['REQUEST_TIME'];
        $this->_nowtime = date("Y-m-d H:i:s",$__now_time);
        $this->_nowdate = date("Y-m-d",$__now_time);
        $user_info = $this->_session->offsetGet ( 'loginInfos' );
        $this->_view->userInfo=$user_info;
    }

    //立即认购 第一步
    public function businessAction(){
        global $_saleOrderType, $_saleOrderSource, $_PurchaseOrderCfg,$_getAreaNames;
        $this->_title = '购物车--填写公司信息';
        $this->_keywords = '购物车';
        $this->_description = '购物车';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $this->_view->cate = $cate;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $this->_view->kd = $kd;
        $id=empty($_GET['id']) ? '0' :(int)$_GET['id'];
        //判断用户是否登录
        if(!$this->_session->has('loginInfos')) {
            $this->redirect('/login');
            die();
        }
        //这边判断id，1.id是否为空，2.id的值和点击购物的报价id是否相同
        if(empty($id)){
            //这边表示报价有问题或客户去掉了id的值
            $this->redirect('/error/error404?error=报价信息为不存在');
            die();
        }else{
            //这边获取session判断是否一致
//            $quote_id = $this->_session->offsetGet ( 'quote_id' );
//            if(!empty($quote_id)){
//                if($id != $quote_id){
//                    $this->redirect('/error/error404');
//                    //这边出错就消除session
////                    $this->_session->offsetSet ( 'quote_id' ,'');
//                    die();
//                }
//            }else{
//                $this->_session->offsetSet ( 'quote_id' ,$id);
//            }
        }
        //获取报价信息
        $quote = new QuotationModel();
        $quotation_info =  $quote->getDetailsById("iq_quotation.id = '{$id}' and iq_quotation.status = '2'");
        //如果没有报价信息，可能非正常信息
        if(empty($quotation_info)){
            $this->redirect('/error/error404?error=报价信息为不存在');
            die();
        }
        //这边还要判断该报价不能是自己的报价
        if(($quotation_info['user_id'] ==$this->_logininfo['id']) && $quotation_info['type'] =='outer'){
            //判断用户的user_id
            Tools::alertBack("亲，这是您自己的报价产品哦!");
            die();
        }

        //设置订单基本信息
        $orderinfo = array(
            'create_uid'=>$this->_logininfo['id'],
            'create_time'=>$this->_nowtime,
            'update_time'=>$this->_nowtime,
            'update_uid'=>$this->_logininfo['id'],
            'order_key'=>"0",
            'business_id'=>"0",
            'user_id'=>$this->_logininfo['id'],//登录的用户id
            'order_from'  =>'sumibuy', // 订单来源
            'order_type'  => $_saleOrderType[$quotation_info['type']], // 订单类型
            'status'  => $_PurchaseOrderCfg['audit'], // 订单类型
            'order_date' => $this->_nowdate, //创建日期
            'organization_id' => '1',// 事业部ID
            'worker_id' => '0',// 管理员ID
            'department_id' => '0', // 部门ID
            'order_amount' => (5*$quotation_info['price']),// 总金额
            'is_join' => 'N',// 是否对接
            'contract_no' => '0',// 合同编号
            'pay_delay' => 'N',// 延长付款
            'pay_join' => 'N',// 是否支付对接
            'pay_type' => '1', // 支付类型
            'deposit_amount' => '0', //
            'is_paid' => 'N',// 是否已经支付
            'checked_time' => '0', // 订单审核通过日期
            'remark' => '0', //
            'pay_amount' => '0',// 已支付的金额
            'price_info' => '',
        );
        //设置订单的明细--产品
        $orderinfo['order_sku'] = array(
            'create_uid' => $this->_logininfo['id'],// 创建人
            'create_time' =>$this->_nowtime,// 创建时间
            'update_time' =>$this->_nowtime,// 更新时间
            'update_uid' =>$this->_logininfo['id'],// 更新人
            'order_id' =>0,// 订单ID
            'product_id'=>$quotation_info['product_id'],
//            'delivery_area'=>$quotation_info['region_name'],//这边的delivery_area被修改为以area_id
            'delivery_area'=>$quotation_info['delivery_area'],//这边的delivery_area被修改为以area_id
            'delivery_time'=>$quotation_info['delivery_time'],
            'service_id'=>$quotation_info['service_id'],
            'currency_id'=>$quotation_info['currency_id'],
            'uom_id' =>'1', // 主单位
            'tax_rate' =>'', // 税率
            'vice_uom_id' =>'0',// 副单位
            'uom_rate' =>'0',// 单位率
            'num' => 5,// 数量
            'price'=>$quotation_info['price'],
            'currency_mark'=>$quotation_info['currency_mark'],
            'amount'=>$quotation_info['price']*5,
            'buy_sku_type'=>$_saleOrderType[$quotation_info['type']],
            'buy_sku_id'=>$id,
            'delivery_method'=>0,
            'delivery_addr'=>'',
            'remark' =>'',// 备注
            'requested_date' =>'0', // 要求的交货时间
            'freight_type' =>'0', //
            'freight' =>'0',//
            'is_join' =>'N',// 是否已对接K3
            'is_pay_join' =>'N',//
            'return_num' =>0,//
            'real_num' =>'',//
            'inout_num' =>0,// 已出库数量
            'tray_type' =>0,// 托盘类型
            'delivery_num' =>0,//
        );
        //设置产品信息
        $orderinfo['product'] = array(
            'cate_name'=>$quotation_info['cate_name'],
            'product_name'=>$quotation_info['product_name'],
            'brand_name'=>$quotation_info['brand_name'],
            'service_name'=>$quotation_info['service_name'],
            'price'=>$quotation_info['price'],
            'num'=>$quotation_info['num'],
            'currency_mark'=>$quotation_info['currency_mark'],
            'delivery_time'=>$quotation_info['delivery_time'],
            'amount'=>$quotation_info['price']*5,
        );
        //设置公司信息
        $orderinfo['business'] = array(
            'order_id'=>'',
            'quotation_id'=>$id,
            'buy_user_id'=>'',
            'sale_user_id'=>'',
            'buy_business_id'=>'',
            'sale_business_id'=>'',
            'buy_business_name'=>'',
            'buy_contacts'=>'',
            'buy_mobile'=>'',
            'sale_business_name'=>'',
            'sale_contacts'=>'',
            'sale_mobile'=>'',
        );
        //设置session
        $this->_session->offsetSet('orderinfo',$orderinfo);

        if(!empty($id)){
            $this->_session->offsetSet('quotation_id',$id);
        }

        //根据报价的id 查询
        $Business = new UsersinvoiceModel();
        $Business_info = $Business->getUserInfoById($this->_logininfo['id'],"","a");//查询所有的公司--fetchall --这是为了方便客户选择公司信息，如果没有公司信息，可直接输入自动创建
        $Business_info_default = $Business->getUserInfoById($this->_logininfo['id'],"Y","r");//这是查询默认信息，展示公司的基本信息

        $this->_view->Business_info = $Business_info;
        $this->_view->Business_info_default = $Business_info_default;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //立即认购 第二步
    public function productAction(){
        global $_saleOrderSource,$_saleOrderType,$_PurchaseOrderCfg,$_getAreaNames ;
        $this->_title = '购物车--填写产品信息';
        $this->_keywords = '购物车';
        $this->_description = '购物车';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $this->_view->cate = $cate;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $this->_view->kd = $kd;
        //判断用户是否登录
        if(!$this->_session->has('loginInfos')) {
            $this->redirect('/login');
            die();
        }
        //获取session--订单信息
        $loginInfo = $this->_session->offsetGet('loginInfos');

        //这边获取公司的id,这是在下一步时，添加的公司id
        $buybusiness =  $this->_session->offsetGet('buy_data');
        //获取订单信息
        $orderinfo = $this->_session->offsetGet('orderinfo');
        if(empty($orderinfo)){
            $this->redirect('/error/error404?error=填写订单信息为空');
            die();
        }
        //获取产品信息
        $product_info = $orderinfo['product'];
        //获取公司信息
        $business_info = $orderinfo['business'];

        //这边获取报价的id
        $quotation_id =  $business_info['quotation_id'];
        //获取报价信息
        $quote = new QuotationModel();
        $quotation_info =  $quote->getDetailsById("iq_quotation.id = '{$quotation_id}'");
        //更新订单的信息
        $order_sku = $orderinfo['order_sku'];//订单的明细信息
        $orderinfo = array(
            'create_uid'=>$orderinfo['create_uid'],
            'create_time'=>$orderinfo['create_time'],
            'update_time'=>$orderinfo['update_time'],
            'update_uid'=>$orderinfo['update_uid'],
            'order_key'=>"0",
            'business_id'=>$buybusiness['business_id'],
            'user_id'=>$orderinfo['user_id'],//登录的用户id
            'order_from'  =>$orderinfo['order_from'], // 订单来源
            'order_type'  => $orderinfo[ 'order_type'], // 订单类型
            'status'  => $orderinfo['status'], // 订单类型
            'order_date' => $orderinfo['order_date'], //创建日期
            'organization_id' => $orderinfo['organization_id'],// 事业部ID
            'worker_id' =>$orderinfo['worker_id'],// 管理员ID
            'department_id' => $orderinfo['department_id'], // 部门ID
            'order_amount' => $orderinfo['order_amount'],// 总金额
            'is_join' => $orderinfo['is_join'],// 是否对接
            'contract_no' => $orderinfo['contract_no'],// 合同编号
            'pay_delay' =>$orderinfo['pay_delay'],// 延长付款
            'pay_join' =>$orderinfo['pay_join'],// 是否支付对接
            'pay_type' => $orderinfo['pay_type'], // 支付类型
            'deposit_amount' =>$orderinfo['deposit_amount'], //
            'is_paid' => $orderinfo['is_paid'],// 是否已经支付
            'checked_time' => $orderinfo['checked_time'], // 订单审核通过日期
            'remark' =>$orderinfo['remark'], //
            'pay_amount' =>$orderinfo['pay_amount'],// 已支付的金额
            'price_info' =>$orderinfo['price_info'],
        );

        $orderinfo['order_sku'] = array(
            'create_uid' => $order_sku['create_uid'],// 创建人
            'create_time' => $order_sku['create_time'],// 创建时间
            'update_time' => $order_sku['update_time'],// 更新时间
            'update_uid' => $order_sku['update_uid'],// 更新人
            'order_id' => $order_sku['order_id'],// 订单ID
            'product_id'=> $order_sku['product_id'],
            'delivery_area'=> $order_sku['delivery_area'],
            'delivery_time'=> $order_sku['delivery_time'],
            'service_id'=> $order_sku['service_id'],
            'currency_id'=> $order_sku['currency_id'],
            'uom_id' => $order_sku['uom_id'], // 主单位
            'tax_rate' => $order_sku['tax_rate'], // 税率
            'vice_uom_id' => $order_sku['vice_uom_id'],// 副单位
            'uom_rate' => $order_sku['uom_rate'],// 单位率
            'num' => $order_sku['num'],// 数量
            'price'=> $order_sku['price'],
            'currency_mark'=> $order_sku['currency_mark'],
            'amount'=> $order_sku['amount'],
            'buy_sku_type'=> $order_sku['buy_sku_type'],
            'buy_sku_id'=> $order_sku['buy_sku_id'],
            'delivery_method'=> $order_sku['delivery_method'],
            'delivery_addr'=> $order_sku['delivery_addr'],
            'remark' => $order_sku['remark'],// 备注
            'requested_date' => $order_sku['requested_date'], // 要求的交货时间
            'freight_type' => $order_sku['freight_type'], //
            'freight' => $order_sku['freight'],//
            'is_join' => $order_sku['is_join'],// 是否已对接K3
            'is_pay_join' => $order_sku['is_pay_join'],//
            'return_num' => $order_sku['return_num'],//
            'real_num' => $order_sku['real_num'],//
            'inout_num' => $order_sku['inout_num'],// 已出库数量
            'tray_type' => $order_sku['tray_type'],// 托盘类型
            'delivery_num' => $order_sku['delivery_num'],//
        );

        $orderinfo['product'] = array(
            'cate_name'=>$product_info['cate_name'],
            'product_name'=>$product_info['product_name'],
            'brand_name'=>$product_info['brand_name'],
            'service_name'=>$product_info['service_name'],
            'price'=>$product_info['price'],
            'num'=>$product_info['num'],
            'currency_mark'=>$product_info['currency_mark'],
            'delivery_time'=>$product_info['delivery_time'],
            'amount'=>$product_info['amount'],
        );

        $orderinfo['business'] = array(
            'order_id'=>$business_info['order_id'],
            'quotation_id'=>$business_info['quotation_id'],
            'buy_user_id'=>$buybusiness['user_id'],
            'sale_user_id'=>$quotation_info['user_id'],
            'buy_business_id'=>$buybusiness['business_id'],
            'sale_business_id'=>$quotation_info['business_id'],
            'buy_business_name'=>$buybusiness['business_name'],
            'buy_business_phone'=>$buybusiness['business_phone'],
            'buy_contacts'=>$buybusiness['contacts'],
            'buy_mobile'=>$buybusiness['mobile'],
            'buy_qq'=>$buybusiness['qq'],
            'sale_business_name'=>empty($quotation_info['business_name'])?"塑料交易网自营":$quotation_info['business_name'],
            'sale_contacts'=>empty($quotation_info['contacts'])?$quotation_info['iq_contacts']:$quotation_info['contacts'],
            'sale_mobile'=>empty($quotation_info['mobile'])?$quotation_info['iq_mobile']:$quotation_info['contacts'],
            'sale_qq'=>empty($quotation_info['qq'])?$quotation_info['iq_qq']:$quotation_info['qq'],
            'sale_business_phone'=>$quotation_info['business_phone'],
        );
        //设置session--更新好订单信息重新设置session
        $this->_session->offsetSet('orderinfo',$orderinfo);
        $this->_view->business_info = $orderinfo['business'];
        $this->_view->product_info = $product_info;
        $this->_view->quotation_info = $quotation_info;
        $this->_view->_getAreaNames = $_getAreaNames;
    }
    //todo 创建订单--这边还没有创建erp的订单
    public function createorderAction(){
        //获取订单信息
        $orderinfo = $this->_session->offsetGet('orderinfo');
        global $_SmsSendSwitch, $_saleOrderType, $_salePayTypeTextcfg, $_saleOrderSourcecfg, $_BuyStateCfg, $_BidPurchaseCfg, $_SmsSendContent;

        //判断订单是否存在
        if(empty($orderinfo)){
            $ret = array(
                'code'  => 404,
                'msg'   => '',
                'data'  => array()
            );
            $this->redirect('/error/error404?error=填写订单信息为空');die();
        }
        //判断用户是否登录
        if(!$this->_session->has('loginInfos')) {
            $this->redirect('/login');
            die();
        }

        //读出买入的公司
//        if(!empty($orderinfo['business_id'])){
//            $ui = new UsersinvoiceModel();
//            $buyuserinfo = $ui->getBusinessInfoById($orderinfo['business_id']);
//        }

        $order = new SaleorderModel();
        //开始插入数据库创建订单
        //1。订单的主表sale-order 2。产品明细表sale_order_sku 3。创建订单和公司的信息sale_order_business 4.更新报价表的数量
        //这边暂时未发送短信
        //这边应该用事物处理
        $db = $order->getDB();
        $db->beginTransaction();
        try {
            $orderdata = array(
                'create_uid'  => $orderinfo['user_id'],//创建人
                'create_time' => $this->_nowtime,// 创建时间
                'update_time' => $this->_nowtime,// 更新时间
                'update_uid'  => $orderinfo['user_id'],// 更新人
                'order_key'   => new Zend_Db_Expr('generateOrderKeyStr()'), // 订单编号
                'user_id'     => $orderinfo['user_id'], // 客户ID
                'business_id' => $orderinfo['business_id'],// 购买公司id
                'order_from'  => $orderinfo['order_from'], // 订单来源
                'order_type'  => $orderinfo['order_type'], // 订单类型
                'order_date'  => $this->_nowdate, //创建日期
                'status'      => $orderinfo['status'],// 订单状态
                'organization_id' => $orderinfo['organization_id'],// 事业部ID
                'worker_id' =>$orderinfo['worker_id'],// 管理员ID
                'department_id' => $orderinfo['department_id'], // 部门ID
                'order_amount' => $orderinfo['order_amount'],// 总金额
                'is_join' => $orderinfo['is_join'],// 是否对接
                'contract_no' => $orderinfo['contract_no'],// 合同编号
                'pay_delay' =>$orderinfo['pay_delay'],// 延长付款
                'pay_join' =>$orderinfo['pay_join'],// 是否支付对接
                'pay_type' => $orderinfo['pay_type'], // 支付类型
                'deposit_amount' =>$orderinfo['deposit_amount'], //
                'is_paid' => $orderinfo['is_paid'],// 是否已经支付
                'checked_time' => $orderinfo['checked_time'], // 订单审核通过日期
                'remark' =>$orderinfo['remark'], //
                'pay_amount' =>$orderinfo['pay_amount'],// 已支付的金额
            );
            $orderID = $order->add($orderdata);

            if(empty($orderID)){
                throw new Exception('创建订单失败！');
            }else{
                $sku = new SaleorderskuModel();
                if(!empty($orderinfo['order_sku'])){
                        $saleordersku=array(
                            'create_uid' =>$orderinfo['user_id'],
                            'create_time' =>$this->_nowtime,
                            'update_time' =>$this->_nowtime,
                            'update_uid' =>$orderinfo['user_id'],
                            'order_id' =>$orderID,
                            'product_id' =>$orderinfo['order_sku']['product_id'],
                            'delivery_area'=>$orderinfo['order_sku']['delivery_area'],
                            'delivery_time'=>$orderinfo['order_sku']['delivery_time'],
                            'service_id'=>$orderinfo['order_sku']['service_id'],
                            'currency_id'=>$orderinfo['order_sku']['currency_id'],
                            'num'=>$orderinfo['order_sku']['num'],
                            'price'=>$orderinfo['order_sku']['price'],
                            'amount'=>($orderinfo['order_sku']['num']*$orderinfo['order_sku']['price']),
                            'buy_sku_type'=>$orderinfo['order_sku']['buy_sku_type'],
                            'buy_sku_id'=>$orderinfo['order_sku']['buy_sku_id'],
                            'delivery_method'=>$orderinfo['order_sku']['delivery_method'],
                            'delivery_addr'=>$orderinfo['order_sku']['delivery_addr'],
                            'remark'=>'',
                            'requested_date' => $orderinfo['order_sku']['requested_date'], // 要求的交货时间
                            'freight_type' => $orderinfo['order_sku']['freight_type'], //
                            'freight' => $orderinfo['order_sku']['freight'],//
                            'is_join' => $orderinfo['order_sku']['is_join'],// 是否已对接K3
                            'is_pay_join' => $orderinfo['order_sku']['is_pay_join'],//
                            'return_num' => $orderinfo['order_sku']['return_num'],//
                            'real_num' => $orderinfo['order_sku']['real_num'],//
                            'inout_num' => $orderinfo['order_sku']['inout_num'],// 已出库数量
                            'tray_type' => $orderinfo['order_sku']['tray_type'],// 托盘类型
                            'delivery_num' => $orderinfo['order_sku']['delivery_num'],//
                        );

                        $affactRows = $sku->add($saleordersku);

                        if($affactRows < 1) throw new Exception('创建订单SKU表失败！');
                        $OrderBusiness = new OrderbusinessModel();
                            $orderbdata = array(
                                'order_id'=>$orderID,
                                'buy_user_id'=>$orderinfo['business']['buy_user_id'],
                                'sale_user_id'=>$orderinfo['business']['sale_user_id'],
                                'buy_business_id'=>$orderinfo['business']['buy_business_id'],
                                'sale_business_id'=>$orderinfo['business']['sale_business_id'],
                                'buy_business_name'=>$orderinfo['business']['buy_business_name'],
                                'buy_business_phone'=>$orderinfo['business']['buy_business_phone'],
                                'buy_contacts'=>$orderinfo['business']['buy_contacts'],
                                'buy_mobile'=>$orderinfo['business']['buy_mobile'],
                                'buy_qq'=>$orderinfo['business']['buy_qq'],
                                'sale_business_name'=>$orderinfo['business']['sale_business_name'],
                                'sale_contacts'=>$orderinfo['business']['sale_contacts'],
                                'sale_mobile'=>$orderinfo['business']['sale_mobile'],
                                'sale_qq'=>$orderinfo['business']['sale_qq'],
                                'sale_business_phone'=>$orderinfo['business']['sale_business_phone'],
                            );
                        $OB =  $OrderBusiness->adddata($orderbdata);
                        if(empty($OB)) throw new Exception('创建订单客户表失败！');
                        //更新扣除库存
                        $order->getDB()->query("update iq_quotation set buy_qty = buy_qty+{$orderinfo['order_sku']['num']} where id='{$orderinfo['business']['quotation_id']}'");

                        //这边的如果是求购报价下单的要修改求购状态
                    if($orderinfo['order_sku']['buy_sku_type']=='require'&&!empty($orderinfo['order_sku']['buy_sku_id'])){
                        $quote = new QuotationModel();
                        $quotation_info =  $quote->getDetailsById("iq_quotation.id = '{$orderinfo['order_sku']['buy_sku_id']}'");
                        if(!empty($quotation_info['inquiry_id'])){
                            $order->getDB()->query("update iq_inquiry set status = '5' where id='{$quotation_info['inquiry_id']}'");
                        }
                    }

                }
            }
            //开启下订单发送短信.这里要注意的是只有库存下单，与第三方下订单。
//            if($_SmsSendSwitch['op_cod'] === 1){
//                if($_SESSION['order_info']['order_type'] == $_saleOrderType['Third']){
//                    $product = $orderinfo['price_info']['cate_name'] ." ". $orderinfo['price_info']['product_name']  ." ". $orderinfo['price_info']['brand_name'];
//                    $phone = $_SESSION['order_info']['mobile'];
//                    $sms = new sendSmsJxt($this->_db_handler,null);
//                    $content = str_replace('%d1',$product,$_SmsSendContent['op_cod_c']);
//                    $sms->otherSend($phone,$content);
//                }
//            }
            $db->commit();
            unset($_SESSION['orderinfo']);
            //这边设置session 为了在成功页面，客户手动输入去查看订单
            $this->_session->offsetSet ( 'order_id' ,$orderID);
            $this->redirect('/shopcart/success?order_id='.$orderID);
            die();
        }catch (Exception $e) {
            $db->rollBack();
//            var_dump($e->getMessage());
            $this->redirect('/error/error404?error='.$e->getMessage());
            die();
        }

    }

    //认购成功
    //todo 这里有个问题就是清楚SESSION
    public function successAction(){
        global $_getAreaNames;
        $order_id = empty($_GET['order_id']) ? '' :htmlspecialchars(trim($_GET['order_id']) , ENT_QUOTES ,'utf-8');
        $order_id = empty($order_id)? 0 :(int)$order_id;
        $this->_title = '购物车';
        $this->_keywords = '购物车';
        $this->_description = '购物车';
        $this->_view->title = $this->_title;
        $this->_view->keywords = $this->_keywords;
        $this->_view->description = $this->_description;
        //检查登录
        if(!$this->_session->has('loginInfos')) {
            $this->redirect('/login');
            die();
        }
        $session_order_id = $this->_session->offsetGet('order_id');
        //检查订单
        if(empty($order_id)){
            $this->redirect('/error/error404?error=订单不存在');
            die;
        }else{
            //这边也要过滤掉
            if($session_order_id !=$order_id ){
                $this->redirect('/error/error404?error=订单信息有错哦，请正确操作！');
                die;
            }
        }
        $loginInfo = $this->_session->offsetGet('loginInfos');
        //这边还要检查该订单的id是否是本人的，这边有可能有人故意输入
        $order = new SaleorderModel();
        $orderinfo = $order->getsaleorder("sale_order.id='{$order_id}' and sale_order.user_id = '{$loginInfo['id']}'");
        if(empty($orderinfo)){
            $this->redirect('/error/error404?error=订单信息不存在');
            die;
        }

        $OrderBusiness = new OrderbusinessModel();
        $OB = $OrderBusiness->getRowByWhere("order_id='{$order_id}'",array("*"));
        $index = new IndexModel();
        $cate = $index->getCateInfo("cate",'','');
        $this->_view->cate = $cate;
        $kd=empty($_GET['kd']) ? '' :htmlspecialchars(trim($_GET['kd']) , ENT_QUOTES ,'utf-8');
        $this->_view->kd = $kd;


        $this->_view->orderinfo = $orderinfo;
        $this->_view->OB = $OB;
        $this->_view->_getAreaNames = $_getAreaNames;
    }

    //更新配送方式
    public function deliveryAction(){
        global $_orderDeliveryTypeMean;
        $this->_logininfo = $this->_session->offsetGet('loginInfos');
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();
            $Post['delivery_method'] = empty($Post['delivery_method']) ? 0 : $Post['delivery_method'];
            //开始设置session
            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['order_sku']['delivery_method'] = $Post['delivery_method'];
            $v = $this->_session->offsetSet('orderinfo',$orderinfo);
            $orderinfo = $this->_session->offsetGet('orderinfo');
            echo json_encode($orderinfo);
            die();
        }
        die();
    }
    //更新配送地址
    public function delivery_addrAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();
            $Post['delivery_addr']  =  empty($Post['delivery_addr']) ? 0 : htmlspecialchars(trim($Post['delivery_addr']), ENT_QUOTES, 'utf-8');
            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['order_sku']['delivery_addr'] = $Post['delivery_addr'];
            $v =  $this->_session->offsetSet('orderinfo',$orderinfo);
            $orderinfo = $this->_session->offsetGet('orderinfo');
            echo json_encode($orderinfo);
            die();
        }
        die();
    }

    //更新数量
    public function updatenumAction(){
        if($this->_request->isPost()){
            $Post = $this->_request->getPost();
            $Post['product_num'] = empty($Post['product_num']) ?  '0' : trim($Post['product_num']);
            $orderinfo = $this->_session->offsetGet('orderinfo');
            $orderinfo['order_sku']['num'] = $Post['product_num'];
            $v = $this->_session->offsetSet('orderinfo',$orderinfo);
            $orderinfo = $this->_session->offsetGet('orderinfo');
            echo json_encode($orderinfo);
            die();
        }
        die();
    }
}